package com.dr.service.impl;

import com.dr.common.pojo.FileFinalValue;
import com.dr.common.ution.FileUtils;
import com.dr.pojo.DirInfo;
import com.dr.pojo.PhtotInfo;
import com.dr.service.PhotoService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tugou on 2017/5/27.
 */
@Service
public class PhotoServiceImpl implements PhotoService {

    @Override
    public List<DirInfo> getAllList(HttpServletRequest request) {
        List<File> list = FileUtils.listFilesInDir(request.getSession().getServletContext().getRealPath(FileFinalValue.SPLITSYMBOL)+"/WEB-INF"+FileFinalValue.SPLITSYMBOL+FileFinalValue.IMGSAVEPATH,false);
        List<DirInfo> dirList = new ArrayList<DirInfo>();
        for (File f:list){
            DirInfo dir = new DirInfo();
            dir.setDirName(f.getName());
            dir.setFileNum(String.valueOf(f.listFiles().length));
            dirList.add(dir);
        }
        return dirList;
    }

    @Override
    public DirInfo getDirByName(String name) {
        return null;
    }

    @Override
    public List<PhtotInfo> getPhotoListByDirName(String dirName, HttpServletRequest request) {
        String photoDir = request.getSession().getServletContext().getRealPath(FileFinalValue.SPLITSYMBOL)+"/WEB-INF"+FileFinalValue.SPLITSYMBOL+FileFinalValue.IMGSAVEPATH+FileFinalValue.SPLITSYMBOL+dirName;
        File f = new File(photoDir);
        File[] files = f.listFiles();
        List<PhtotInfo> list = new ArrayList<>();
        if(files != null && files.length>0){
            for (File e:files){
                PhtotInfo info = new PhtotInfo();
                info.setDirName(dirName);
                info.setPath(FileFinalValue.IMGSAVEPATH+"/"+dirName+"/"+e.getName());
                info.setName(e.getName());
                list.add(info);
            }
            return list;
        }else{
            return null;
        }
    }


}
